﻿using Configs;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class InputReader {
	static InputReader() {
		InputConfigDic.Add(0, new InputConfig());
		InputConfigDic.Add(1, new InputConfig(new KeyCode[] {
			KeyCode.UpArrow,
			KeyCode.DownArrow,
			KeyCode.LeftArrow,
			KeyCode.RightArrow,
			KeyCode.LeftShift,
			KeyCode.Space,
			KeyCode.LeftControl,
			KeyCode.R,
			KeyCode.E,
			KeyCode.O,
		}));
	}
	public static Dictionary<int, InputConfig> InputConfigDic = new Dictionary<int, InputConfig>();
	public static bool Up { get { return CheckInput(Action.Up); } }
	public static bool Down { get { return CheckInput(Action.Down); } }
	public static bool Left { get { return CheckInput(Action.Left); } }
	public static bool Right { get { return CheckInput(Action.Right); } }
	public static bool Jump { get { return CheckInputDown(Action.Jump); } }
	public static bool Run { get { return CheckInput(Action.Run); } }
	public static bool Pull { get { return CheckInput(Action.Pull); } }
	public static bool Crouch { get { return CheckInput(Action.Crouch); } }
	public static bool InterActive { get { return CheckInput(Action.InterActive); } }
	public static bool ChangeController { get { return CheckInputDown(Action.ChangeController); } }

	public static int HorizontalRaw {
		get {
			if (CheckInput(Action.Right))
				return 1;
			if (CheckInput(Action.Left))
				return -1;
			return 0;
		}
	}
	public static int VerticalRaw{
		get {
			if (CheckInput(Action.Down))
				return -1;
			if (CheckInput(Action.Up))
				return 1;
			return 0;
		}
	}

	private static bool CheckInput(Action index) {
		foreach (var item in InputConfigDic.Values) {
			if (Input.GetKey(item.keyCodes[(int)index])) {
				return true;
			}
		}
		return false;
	}	
	private static bool CheckInputDown(Action index) {
		foreach (var item in InputConfigDic.Values) {
			if (Input.GetKeyDown(item.keyCodes[(int)index])) {
				return true;
			}
		}
		return false;
	}

}
namespace Configs {
	public class InputConfig {
		public static readonly int Count = 9;
		public KeyCode[] keyCodes { get; private set; } = new KeyCode[] {
			KeyCode.W,
			KeyCode.S,
			KeyCode.A,
			KeyCode.D,
			KeyCode.LeftShift,
			KeyCode.Space,
			KeyCode.LeftControl,
			KeyCode.I,
			KeyCode.E,
			KeyCode.O,
		};

        public InputConfig()
        {
        }

        public InputConfig(KeyCode[] keyCodes)
        {
            this.keyCodes = keyCodes;
        }
    }

	enum Action {
		Up,
		Down,
		Left,
		Right,
		Run,
		Jump,
		Crouch,
		Pull,
		InterActive,
		ChangeController,
	}

}

